﻿using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace KillBill.Common.Validation
{
    public class DataDictionary
    {
        private static Dictionary<DataType, Regex> _typeDictionary = new Dictionary<DataType, Regex>();
        private Regex _customRegex = new Regex(".*");
        private DataType _type = DataType.String;

        public DataType Type
        {
            get { return _type; }
            set { _type = value; }
        }
        public  Regex Expression
        {
            get { return _type == DataType.Custom ? _customRegex : TypeDictionary[_type]; }
            set
            {
                if (_type == DataType.Custom)
                    _customRegex = value;
            }
        }
        public static Dictionary<DataType, Regex> TypeDictionary
        {
            get
            {
                if (_typeDictionary==null || _typeDictionary.Count==0)
                {
                    _typeDictionary = new Dictionary<DataType, Regex>
                                          {
                                              {DataType.String, new Regex(@".*")},
                                              {DataType.Integer, new Regex("^[0-9]+$")},
                                              {DataType.Number, new Regex(@"^[0-9]+((\.|,)[0-9]+){0,1}$")},
                                              {DataType.IdNumber, new Regex(@"^[0-9]{10,13}$")},
                                              {DataType.AutoMovilId, new Regex("^[A-Z]{3}-[0-9]{3,4}$")},
                                              {DataType.Ruc, new Regex(@"^[0-9]{10}([0-9]{3}){0,1}$")},
                                              {DataType.OnlyLetters, new Regex(@"^([A-z]|[a-z]|[á-ú]|\s)+$")},
                                              {DataType.EMail, new Regex(@"(\w|\.|-)+@\w+(\.\w+)+")},
                                              {DataType.PhoneNumber, new Regex(@"^0[0-9]{8}$")}
                                          };
                }
                return _typeDictionary;
            }
        }
        public static bool ValidateValue(string value, DataType dataType)
        {
            return dataType == DataType.IdNumber ? Ecuasoft.Seguridad.ValidacionRUC_CI.ValidarDocumento(value) : TypeDictionary[dataType].IsMatch(value);
        }

        public bool ValidateLocalValue(string value, DataType dataType)
        {
            return dataType == DataType.Custom ? _customRegex.IsMatch(value) : ValidateValue(value, dataType);
        }
    }
}
